package net.NingXiaRuiTai.ShortChain.controller;


import net.NingXiaRuiTai.ShortChain.controller.request.AccountRegisterRequest;
import net.NingXiaRuiTai.ShortChain.controller.request.LoginRequest;
import net.NingXiaRuiTai.ShortChain.enums.BizCodeEnum;
import net.NingXiaRuiTai.ShortChain.server.AccountService;
import net.NingXiaRuiTai.ShortChain.server.FileService;
import net.NingXiaRuiTai.ShortChain.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yuan
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/api/account/v1")
public class AccountController {
    @Autowired
    private FileService fileService;
    @Autowired
    private AccountService accountService;




    /*
    处理用户上传图像的请求
    注意：默认SpringBoot最⼤⽂件上传是1M
     *
     * @param file @requestPart注解 接收⽂件以及其他更为复杂的数据类型
     * @return 表示操作成功的JsonData对象
     *
    */
    @PostMapping("upload")
    public JsonData UploadUserImag(@RequestPart("file") MultipartFile file){
        String result = fileService.uploadUserImag(file);
        return result !=null ? JsonData.buildSuccess(result):JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }
    /**
     * 处理用户注册请求
     *
     * @param request 包含用户注册信息的请求对象
     * @return 表示注册操作结果的JsonData对象
     */

    @PostMapping("register")
    public JsonData register(@RequestBody AccountRegisterRequest request){
       JsonData jsonData= accountService.register(request);
        return jsonData;
    }
    @PostMapping("login")
    public JsonData login(@RequestBody LoginRequest loginRequest) {
        JsonData jsonData= accountService.login(loginRequest);
        return jsonData;
    }
}

